package br.com.esporte.business.impl.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import br.com.esporte.model.DiaSemanaEnum;
import br.com.esporte.model.HoraEnum;
import br.com.esporte.model.MesEnum;
import br.com.esporte.model.MinutoEnum;

public class DateConverter {

	public static Date converterDataHora(Date date, HoraEnum hora, MinutoEnum minuto){
		Date data = null;
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			StringBuilder dataString = new StringBuilder();
			dataString.append(calendar.get(Calendar.DAY_OF_MONTH));
			dataString.append("/");
			dataString.append(calendar.get(Calendar.MONTH) + 1);
			dataString.append("/");
			dataString.append(calendar.get(Calendar.YEAR));
			dataString.append(" ");

			int horaValor = hora.getValor();
			if(horaValor >= 10){
				dataString.append(horaValor);
			}else{
				dataString.append("0" + horaValor);
			}

			dataString.append(":");

			int minutoValor = minuto.getValor();
			if(minutoValor >= 10){
				dataString.append(minutoValor);
			}else{
				dataString.append("0" + minutoValor);
			}

			SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");


			data = dataFormat.parse(dataString.toString());
		} catch (Exception e) {

		}
		return data;
	}

	public static String converterDataHoraString(Date data,  HoraEnum horaInicio, MinutoEnum minutoInicio){
		StringBuilder dataString = new StringBuilder();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(data);
		dataString.append(calendar.get(Calendar.DAY_OF_MONTH));
		dataString.append("/");
		dataString.append(calendar.get(Calendar.MONTH) + 1);
		dataString.append("/");
		dataString.append(calendar.get(Calendar.YEAR));
		dataString.append(" ");

		if(horaInicio.getValor() < 10){
			dataString.append("0");
		}

		dataString.append(horaInicio.getValor());
		dataString.append(":");
		if(minutoInicio.getValor() < 10){
			dataString.append("0");
		}
		dataString.append(minutoInicio.getValor());

		return dataString.toString();
	}

	public static String converterDataHoraToString(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		StringBuilder dataString = new StringBuilder();

		int dia = calendar.get(Calendar.DAY_OF_MONTH);
		if(dia < 10){
			dataString.append("0");
		}
		dataString.append(dia);
		dataString.append("/");

		int mes = calendar.get(Calendar.MONTH) + 1;
		if(mes < 10){
			dataString.append("0");
		}
		dataString.append(mes);
		dataString.append("/");

		dataString.append(calendar.get(Calendar.YEAR));
		dataString.append(" ");

		int hora = calendar.get(Calendar.HOUR);
		if(hora < 10){
			dataString.append("0");
		}
		dataString.append(hora);
		dataString.append(":");

		int minuto = calendar.get(Calendar.MINUTE);
		if(minuto < 10){
			dataString.append("0");
		}
		dataString.append(minuto);

		return dataString.toString();
	}

	public static String converterDataToString(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		StringBuilder dataString = new StringBuilder();
		int dia = calendar.get(Calendar.DAY_OF_MONTH);
		if(dia < 10){
			dataString.append("0");
		}
		dataString.append(dia);
		dataString.append("/");

		int mes = calendar.get(Calendar.MONTH) + 1;
		if(mes < 10){
			dataString.append("0");
		}
		dataString.append(mes);
		dataString.append("/");

		dataString.append(calendar.get(Calendar.YEAR));
		return dataString.toString();
	}

	public static String converterHoraToString(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		StringBuilder dataString = new StringBuilder();

		int hora = calendar.get(Calendar.HOUR);
		if(hora < 10){
			dataString.append("0");
		}
		dataString.append(hora);

		dataString.append(":");

		int minuto = calendar.get(Calendar.MINUTE);
		if(minuto < 10){
			dataString.append("0");
		}
		dataString.append(minuto);

		return dataString.toString();
	}

	public static Date converterStringToData(String dataString){
		SimpleDateFormat dataFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		Date data = null;
		try {
			data = dataFormat.parse(dataString);
		} catch (ParseException e) {

		}
		return data;
	}

	public static String coverterDataStringExtenso(Date date){
		StringBuilder dataString = new StringBuilder();

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		dataString.append(DiaSemanaEnum.getDia(calendar.get(Calendar.DAY_OF_WEEK)).getDescricao());
		dataString.append(", ");
		dataString.append(calendar.get(Calendar.DAY_OF_MONTH));
		dataString.append(" de ");
		dataString.append(MesEnum.getMes(calendar.get(Calendar.MONTH)).getDescricao());
		dataString.append(" de ");
		dataString.append(calendar.get(Calendar.YEAR));
		return dataString.toString();
	}

}
